package com.qk.ezbuy.controller;

import com.qk.ezbuy.dao.ProductMapper;
import com.qk.ezbuy.pojo.Product;
import io.swagger.models.auth.In;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/solr")
public class SolrController {
    @Autowired
    private SolrClient solrClient;
    @Autowired
    private ProductMapper productMapper;

    @RequestMapping("/select")
    public Map<String,Object> solrHigh(@RequestParam Map<String,Object> map){
        SolrQuery solrQuery = new SolrQuery();
        Integer pageNum = Integer.parseInt(map.get("pageNum").toString());
        Integer pageSize =Integer.parseInt(map.get("pageSize").toString());
        //查询关键词
        if(map.get("solr") != null && !map.get("solr").equals("")){
            solrQuery.setQuery("name_ik:"+map.get("solr")+"");
            solrQuery.setHighlight(true);
            solrQuery.addHighlightField("name_ik");
            solrQuery.setHighlightSimplePre("<font color=\"red\">");
            solrQuery.setHighlightSimplePost("</font>");
        }else{
            solrQuery.setQuery("*:*");
        }
        //排序条件

        if(map.get("opr")!=null){
            if(map.get("opr").equals("priceup")){
                solrQuery.addSort("price_f", SolrQuery.ORDER.asc);
            }else if(map.get("opr").equals("pricedown")){
                solrQuery.addSort("price_f", SolrQuery.ORDER.desc);
            }else if(map.get("opr").equals("stockup")){
                solrQuery.addSort("stock_i", SolrQuery.ORDER.asc);
            }else if(map.get("opr").equals("stockdown")){
                solrQuery.addSort("stock_i", SolrQuery.ORDER.desc);
            }
        }
        solrQuery.setStart((pageNum-1)*pageSize);
        solrQuery.setRows(pageSize);

        List<Product> list =null;

        try {
            QueryResponse qr = solrClient.query(solrQuery);
            list = qr.getBeans(Product.class);
            //获取总条数
            SolrDocumentList sdl = qr.getResults();
            int count = (int)sdl.getNumFound();
            map.put("count",count);
            map.put("pages",count%pageSize==0 ? count/pageSize : count/pageSize+1);
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            if(map.get("solr") != ""){
                Map<String, Map<String,List<String>>> mapl = qr.getHighlighting();
                for (Product product: list){
                    product.setName(mapl.get(product.getId()).get("name_ik").get(0));
                }
            }
            map.put("list",list);
            return map;
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }
}
